//
//  Util.m
//  YiChat20160706
//
//  Created by 李政 on 16/7/6.
//  Copyright © 2016年 ZhiYou. All rights reserved.
//

#import "Util.h"

@implementation Util


+ (void)showAlertWithMessage:(NSString *)message confirmBlock:(Block)block
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ALERT_TITLE message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:ACTION_TITLE style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (block)
        {
            block();
        }
    }];
    [alertController addAction:action];
    
    [WINDOW.rootViewController presentViewController:alertController animated:YES completion:nil];
}



+ (NSString *)getDestinationFilePathWithFile:(NSDictionary *)fileDic

{
    NSString *desPath = nil;
    
    
    
    if ([fileDic[@"type"] isEqualToString:@"4"]) {
        
        desPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Image/%@",fileDic[@"tname"]]];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Image"];
        
        if (![fileManager fileExistsAtPath:imagePath isDirectory:nil]) {
            
            [fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
            
        }
        
    }else
    {
        
        desPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/Other/%@",fileDic[@"tname"]]];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        
        NSString *otherPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Other"];
        
        if (![fileManager fileExistsAtPath:otherPath isDirectory:nil]) {
            
            [fileManager createDirectoryAtPath:otherPath withIntermediateDirectories:YES attributes:nil error:nil];
            
        }
    }
    return desPath;
}


//检查文件是否已经下载
+ (BOOL)checkTheFileDidDownloadWithFile:(NSDictionary *)fileDic
{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *filePath = [self getDestinationFilePathWithFile:fileDic];
    
    if ([fileManager fileExistsAtPath:filePath]) {
        
        return YES;
    }
    
    return NO;
}

+ (NSString *)getTempFilePathWithFile:(NSDictionary *)fileDic
{
    NSString *tmpPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",fileDic[@"tname"]]];
    
    
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    if (![mgr fileExistsAtPath:tmpPath])
    {
        [mgr createFileAtPath:tmpPath contents:nil attributes:nil];
    }
    
    return tmpPath;
}

+ (long long)getFileSizeWithFile:(NSDictionary *)fileDic
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *filePath = [self getTempFilePathWithFile:fileDic];
    
    
    NSDictionary *dic = [fileManager attributesOfItemAtPath:filePath error:nil];
    
    return [dic[NSFileSize] longLongValue];
}

+ (BOOL)deleteFileWithDic:(NSDictionary *)dic
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *filePath = [self getDestinationFilePathWithFile:dic];
    
    
    return [fileManager removeItemAtPath:filePath error:nil];
}

@end
